package com.sry.blog.sys.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sry.blog.common.api.CommonResult;
import com.sry.blog.dto.CommentDTO;
import com.sry.blog.dto.ContentDTO;
import com.sry.blog.dto.ReplyDTO;
import com.sry.blog.sys.model.Content;
import com.sry.blog.sys.model.User;
import com.sry.blog.sys.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * InnoDB free: 10240 kB; (`article_id`) REFER `blog/article`(`id`) ON UPDATE CASCA 前端控制器
 * </p>
 *
 * @author SR_Y
 * @since 2022-01-05
 */
@RestController
@RequestMapping("/content")
public class ContentController {

    @Autowired
    private ContentService contentService;
    @Autowired
    private UserController userController;
    @Autowired
    private ArticleController articleController;

    @RequestMapping("/getContent")
    public Content getContent(Integer id) {
        return contentService.getById(id);
    }


    /**
     * 保存评论
     *
     * @return boolean
     * @return
     * @throws Exception
     * @author SR_Y
     */
    @RequestMapping("/saveComment")
    @ResponseBody
    public boolean saveComment(@RequestBody Content content) {
        //把contentisdelete设置为0，time设置为当前时间  并且把articleid设置为articleid 并且把userid设置为userid 并且把contenttype设置为1
        content.setIsdelete(0);
        content.setTime(new Date());
        return contentService.creatOrUpdate(content);
    }

    /**
     * 逻辑删除评论
     *
     * @return boolean
     * @return
     * @throws Exception
     * @author SR_Y
     */
    @RequestMapping("/delete")
    public boolean delComment(@RequestBody Content content) {
        content.setIsdelete(1);
        return contentService.creatOrUpdate(content);
    }

    /**
     * 添加文章评论
     */
    @RequestMapping(value = "/addComment", method = RequestMethod.POST)
    public CommonResult addComment(@RequestBody Content content) {
        content.setIsdelete(0);
        content.setTime(new Date());
        content.setType(0);
        return contentService.creatOrUpdate(content) ? CommonResult.success(true) : CommonResult.failed();
    }

    /**
     * 添加回复评论
     */
    @RequestMapping("/addReply")
    public boolean addReply(@RequestBody Content content) {
        content.setIsdelete(0);
        content.setTime(new Date());
        content.setType(1);
        return contentService.creatOrUpdate(content);
    }

    /**
     * 获取文章评论
     *
     * @return
     */
    @RequestMapping(value = "/getComment", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<ContentDTO>> getComment(@RequestParam Integer id) {
        List<Content> list = contentService.list(Content.builder().build().setArticleId(id));
        //新建一个ContentDTO的list
        List<ContentDTO> contentDTOList = new ArrayList<>();
        for (Content c : list) {
            //把reply_id为零的对象取出来
            if (c.getReplyId() == 0) {
                User user = userController.findById(c.getUserId());
                List<ReplyDTO> replyDTOS = new ArrayList<>();
                for (Content reply : list) {
                    Content initID = findInitID(reply);
                    if (null != initID && initID.getId() == c.getId() && reply.getReplyId() != 0) {
                        User user1 = userController.findById(reply.getUserId());
                        User user2 = userController.findById(contentService.getById(reply.getReplyId()).getUserId());
                        replyDTOS.add(ReplyDTO.builder().id(reply.getId()).date(reply.getTime()).content(reply.getContent()).fromId(user1.getId()).fromName(user1.getNickName()).fromAvatar(user1.getUserAvatar()).toId(user2.getId()).toName(user2.getNickName()).toAvatar(user2.getUserAvatar()).build());
                    }
                }
                //新建一个ContentDTO
                contentDTOList.add(ContentDTO.builder().id(c.getId()).content(c.getContent()).ownerId(c.getArticleId()).fromId(user.getId()).fromName(user.getNickName()).fromAvatar(user.getUserAvatar()).date(c.getTime()).replys(replyDTOS).build());
            }

        }
        return CommonResult.success(contentDTOList);
    }

    /**
     * 查找初始id
     *
     * @return
     */
    public Content findInitID(Content content) {
        Integer replyId = content.getReplyId();
        if (0 != replyId) {
            return findInitID(contentService.getById(replyId));
        } else {
            return content;
        }
    }

    @RequestMapping(value = "/getCommentTable", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Page<CommentDTO>> getCommentTable(@RequestBody Content comment, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize) {
        Page<Content> commentTable = contentService.getCommentTable(comment, pageNum, pageSize);
        List<CommentDTO> commentDTOS = new ArrayList<>();
        for (Content c : commentTable.getRecords()) {
            commentDTOS.add(CommentDTO.builder().comment(c).userName(userController.findById(c.getUserId()).getUserName()).articleTitle(articleController.getOneById(c.getArticleId()).getTitle()).build());
        }
        Page<CommentDTO> commentDTOPage = new Page<>(pageNum, pageSize);
        commentDTOPage.setRecords(commentDTOS);
        commentDTOPage.setTotal(commentTable.getTotal());
        return CommonResult.success(commentDTOPage);
    }


    @RequestMapping(value = "/deleteComment", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@RequestBody Content comment) {
        return CommonResult.success(contentService.creatOrUpdate(comment));
    }

}



